let table = null;
let eData, dataCount;
layui
    .config({
        base: '../layui/exts/', //静态资源所在路径
    })
    .use(['table', 'form', 'dropdown', 'util'], function () {
        lui = layui;
        var form = layui.form, dropdown = layui.dropdown;
        $ = layui.$;
        form.render();


        //自定义事件 - hover
        dropdown.render({
            elem: '#recordBatchEdit'
            , trigger: 'hover'
            , data: [{
                title: '批量修改'
                , id: 'batchEdit'
            }, {
                title: '批量删除'
                , id: 'batchDel'
            }], click: function (obj, othis) {
                var data = table.checkStatus("dataRecordTable").data;
                if (data.length === 0) {
                    top.parent.layer.alert("请选择需要处理的数据", {icon: 2});
                    return false;
                }
                var ids = [];
                for (let i = 0; i < data.length; i++) {
                    ids.push(data[i].id);
                }
                if (obj.id === 'batchEdit') {
                    batchModify("1", ids);
                } else if (obj.id === 'batchDel') {
                    batchDelete("1", ids);
                }
            }
        });


        //创建表格
        table = layui.table;
        //文件表格初始化
        let recordCols = []
        let recordCol = []
        $.ajax({
            type: 'POST',
            url: '/archivesClass/getShowFieldList',
            dataType: "JSON",
            sync: false,
            data: {"classifyId": classifyId, "fillingModel": "1"},
            success: function (res) {
                recordCol.push({type: 'checkbox', fixed: 'left'});
                $.each(res.data, function (index, obj) {
                    let leftFixed = "";
                    if (obj.fixedLeft == "1") {
                        leftFixed = "left";
                    }
                    recordCol.push({
                        fixed: leftFixed,
                        field: obj.fieldName,
                        width: obj.showWidth,
                        title: obj.fieldTitle
                    });
                });
                recordCol.push({fixed: 'right', title: '操作', width: 110, toolbar: '#recordOperationBar'});
                recordCols.push(recordCol);
                openRecordData();
            }
        });


        form.on('submit(recordSearch)', function (data) {
            value = data.field.value;
            openRecordData(value);
            return false;
        });

        function openRecordData(value) {
            let data = [];
            if (value != undefined) {
                data["value"] = value;
            }
            let div = document.getElementById('recordData');
            let divHeight = div.clientHeight - 47;
            let url = '../archives/getArchivesRecord?classifyId=' + classifyId + '&pid=' + pid + '&tid=' + tid;
            table.render({
                elem: '#recordDataTable'
                , url: url
                , contentType: 'application/json'
                , method: 'post'
                , cellMinWidth: 80
                , page: true
                , limit: 50
                , height: divHeight
                , loading: true
                , size: 'sm'
                , where: data
                , cols: recordCols
                , done: function (res, curr, count) {
                    dataCount = count;
                    let eData = res.data;
                }
            });
        }

        form.on('submit(recordsSearch)', function (data) {
            rValue = data.field.value;
            openRecordData();
            return false;
        });


        //监听工具条
        table.on('tool(recordDataTable)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'edit':
                    top.layer.open({
                        type: 2,
                        title: '编辑档案信息',
                        content: './collection/setArchives?archivesModel=1&id=' + data.id + '&tid=' + tid +
                            '&classifyId=' + classifyId + '&pid=' + pid,
                        area: ['550px', $(top.window).height() + 'px'],
                        shadeClose: false,
                        resize: false,
                        offset: 'rt',
                        btn: [
                            '<i class="layui-icon">&#xe605;</i> 确定',
                            '<i class="layui-icon">&#x1006;</i> 取消',
                        ],
                        yes: function (index, layero) {
                            var iframeWin = layero.find('iframe')[0];
                            if (iframeWin.contentWindow.Buttun_Submit()) {
                                table.reload('recordDataTable');
                            }
                        },
                        btn2: function (index) {
                            layer.close(index); //如果设定了yes回调，需进行手工关闭
                        },
                    });
                    break;
                case 'del':
                    top.layer.confirm('确定要删除当前选择的文件吗？', function (index) {
                        $.ajax({
                            url: '../collection/delArchives', //url
                            dataType: 'json', //数据格式
                            type: 'POST', //请求方式
                            async: false, //是否异步请求
                            data: {id: data.id, pid: pid, tid: tid, archivesModel: "1"},
                            success: function (result) {
                                if (result.code == 0) {
                                    obj.del();
                                    layer.close(index);
                                    top.parent.layer.msg(result.message, {icon: 1});
                                } else {
                                    top.parent.layer.alert(result.message, {icon: 2});
                                }
                            },
                        });
                        return false;
                    });
                    break;
                case 'viewDigiFiles':
                    top.layer.open({
                        type: 2,
                        title: data.title + "  电子文件",
                        content: '../archives/archivesOriginalFiles?archivesId=' + data.id,
                        area: [$(top.parent.window).width() + 'px', $(top.parent.window).height() + 'px'],
                        shadeClose: false,
                        resize: false,
                        offset: 'r',
                    });
                    break;
                case  'changelog':
                    top.layer.open({
                        type: 2,
                        title: "档案追溯日志",
                        content: '../archives/changelog?archivesId=' + data.id,
                        area: ['500px', $(parent.window).height() + 'px'],
                        shadeClose: false,
                        resize: false,
                        offset: 'r',
                    });
                    break;
            }
        });
    });


function loadTemplate() {
    top.layer.confirm("载入模板将会清除现有的数据，确定要使用模板数据吗？", function (index) {
        $.ajax({
            url: './loadTemplateData', //url
            dataType: 'json', //数据格式
            type: 'POST', //请求方式
            async: false, //是否异步请求
            data: {pid: pid, tid: tid, fileId: fileId, classifyId: classifyId},
            success: function (result) {
                if (result.code == 0) {
                    top.parent.layer.alert(result.message, {icon: 1});
                    table.reload('dataRecordTable');
                    layer.close(index);
                } else {
                    top.parent.layer.alert(result.message, {icon: 2});
                }
            },
        });
        return false;
    });
}

function newRecords() {
    if (fileId == "") {
        top.parent.layer.alert("请选择需要增加文件的案卷", {icon: 2});
        return false;
    }
    top.layer.open({
        type: 2,
        title: '编辑档案信息',
        content: './collection/setArchives?archivesModel=1&pid=' + pid + '&classifyId=' + classifyId +
            '&tid=' + tid + '&fileId=' + fileId,
        area: ['550px', $(top.window).height() + 'px'],
        shadeClose: false,
        resize: false,
        offset: 'rt',
        btn: [
            '<i class="layui-icon">&#x1005;</i> 保存/新增',
            '<i class="layui-icon">&#xe605;</i> 确定',
            '<i class="layui-icon">&#x1006;</i> 取消',
        ],
        yes: function (index, layero) {
            var iframeWin = layero.find('iframe')[0];
            if (iframeWin.contentWindow.Buttun_SaveSubmit()) {
                table.reload('dataRecordTable');
            }
            return false;
        },
        btn2: function (index, layero) {
            var iframeWin = layero.find('iframe')[0];
            if (iframeWin.contentWindow.Buttun_Submit()) {
                table.reload('dataRecordTable');
            } else {
                return false;
            }
        },
        btn3: function (index) {
            layer.close(index); //如果设定了yes回调，需进行手工关闭
        },
    });
}

